package net.realsee.service.system.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.realsee.domain.system.SysJobLog;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.system.SysJobLogMapper;
import net.realsee.service.system.ISysJobLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 定时任务调度日志信息 服务层
 *
 * @author realsee
 */
@Service
public class SysJobLogServiceImpl implements ISysJobLogService {

    @Autowired
    private SysJobLogMapper jobLogMapper;

    @Override
    public TableDataInfo<SysJobLog> pageJobLogList(SysJobLog sysJobLog, PageQuery pageQuery) {
        Page<SysJobLog> result = jobLogMapper.selectVoPage(pageQuery.build(), buildQueryWrapper(sysJobLog));
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<SysJobLog> buildQueryWrapper(SysJobLog sysJobLog) {
        Map<String, Object> params = sysJobLog.getParams();
        LambdaQueryWrapper<SysJobLog> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(sysJobLog.getJobName()), SysJobLog::getJobName, sysJobLog.getJobName());
        lqw.eq(StrUtil.isNotBlank(sysJobLog.getJobGroup()), SysJobLog::getJobGroup, sysJobLog.getJobGroup());
        lqw.eq(StrUtil.isNotBlank(sysJobLog.getStatus()), SysJobLog::getStatus, sysJobLog.getStatus());
        if (CollUtil.isNotEmpty(params)) {
            lqw.le(params.containsKey("endTime"), SysJobLog::getCreateTime, params.get("endTime") + " 23:59:59");
            lqw.ge(params.containsKey("beginTime"), SysJobLog::getCreateTime, params.get("beginTime") + " 00:00:00");
        }
        lqw.orderByDesc(SysJobLog::getJobLogId);
        return lqw;
    }

    /**
     * 获取quartz调度器日志的计划任务
     *
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public List<SysJobLog> selectJobLogList(SysJobLog jobLog) {
        return jobLogMapper.selectJobLogList(jobLog);
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    @Override
    public SysJobLog selectJobLogById(Long jobLogId) {
        return jobLogMapper.selectJobLogById(jobLogId);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(SysJobLog jobLog) {
        jobLogMapper.insertJobLog(jobLog);
    }

    /**
     * 批量删除调度日志信息
     *
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobLogByIds(Long[] logIds) {
        return jobLogMapper.deleteJobLogByIds(logIds);
    }

    /**
     * 删除任务日志
     *
     * @param jobId 调度日志ID
     */
    @Override
    public int deleteJobLogById(Long jobId) {
        return jobLogMapper.deleteJobLogById(jobId);
    }

    /**
     * 清空任务日志
     */
    @Override
    public void cleanJobLog() {
        jobLogMapper.cleanJobLog();
    }
}
